﻿
namespace SADev.SharePointHolidayLoader
{
    using System;
    using System.Collections.Generic;
    using System.Linq;

    internal class CountryList : IEnumerable<Country>
    {
        private readonly IList<Country> countries = new List<Country>();

        public Country this[int index]
        {
            get
            {
                return countries[index];
            }
        }

        public Country this[string name]
        {
            get
            {
                return countries.Where(c => c.Name.Equals(name, StringComparison.CurrentCultureIgnoreCase)).Single();
            }
        }

        public IEnumerator<Country> GetEnumerator()
        {
            return new CountryListEnumerator(countries);
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return new CountryListEnumerator(countries);
        }

        private class CountryListEnumerator : IEnumerator<Country>
        {
            private readonly IList<Country> countries;
            private int position = -1;

            public CountryListEnumerator(IList<Country> countries)
            {
                this.countries = countries;
            }
            public Country Current
            {
                get
                {
                    return countries[position];
                }
            }

            public void Dispose()
            {
                // n/a
            }

            object System.Collections.IEnumerator.Current
            {
                get
                {
                    return countries[position];
                }
            }

            public bool MoveNext()
            {
                position++;
                return position < countries.Count;
            }

            public void Reset()
            {
                position = -1;
            }
        }

        internal void Add(Country country)
        {
            this.countries.Add(country);
        }
    }
}
